package com.itheima.mm.controller;

import com.itheima.mm.pojo.permission.Permission;
import com.itheima.mm.dto.PermissionMenu;
import com.itheima.mm.dto.PermissionPoint;
import com.itheima.mm.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/menus")
public class PermissionController {
     @Autowired
     private PermissionService permissionService;
    @PutMapping("/{id}")
    public String motifyPermission(@PathVariable Integer id, @RequestBody PermissionPoint permissionPoint){
          //修改permission的数据
        Permission permission = new Permission();
        permission.setName(permissionPoint.getTitle());
        permission.setPid(permissionPoint.getPid());
        permission.setId(permissionPoint.getId());
        permissionService.updateById(permission);
        //根据ispoint来修改code数据
        permissionService.updateCode(permissionPoint);
        return  "修改成功";
    }
    //展示菜单列表的数据
    @GetMapping
    public List<PermissionMenu> echo(){

      List<PermissionMenu> list=permissionService.selectlist();
      return list;

    }

    @DeleteMapping("/{id}")
    public  String delete (@PathVariable Integer id){


        return   permissionService.delete(id);
    }

    @PostMapping
    public  String add(@RequestBody PermissionPoint permissionPoint){

        return   permissionService.add(permissionPoint);
    }
    @GetMapping("/{id}")
    public PermissionPoint CaiDanXiangQing(@PathVariable Integer id){


        return  permissionService.selectById(id);
    }
}
